pub mod auth;
pub mod rbac;
pub mod resource;
pub mod role;
pub mod user;

use std::error::Error;

use axum_app_common::error::BizError;
use serde::de::DeserializeOwned;
use serde_json::Value;
use tracing::error;
use validator::{Validate, ValidationError, ValidationErrors};
pub fn json_value_to<'de, T>(value: Value) -> anyhow::Result<T>
where
    T: DeserializeOwned + Validate,
{
    let res = match serde_json::from_value::<T>(value) {
        Ok(v) => v,
        Err(err) => {
            let calssify = err.classify();
            let column = err.column();
            let source = err.source();

            eprintln!(
                "classify={:?},column={:?}, source={:?}, err={:?} ",
                calssify, column, source, err
            );

            anyhow::bail!(BizError::DeserializeError)
        }
    };

    // 校验
    res.validate()?;

    Ok(res)
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use crate::model::json_value_to;

    use crate::model::user::UserQueryPageParams;

    #[test]
    fn test_json_value_to() {
        let json_v = json!({"username":" ", "age":1});
        let obj = json_value_to::<UserQueryPageParams>(json_v);

        println!("{:?}", obj);
    }
}
